package pl.umk.mat.fred.unicore.urc.storagemanagement.tree.sorters;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;

import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.LabelProviderImplementation;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.CatalogNode;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.FileNode;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.GridNode;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.MultiplyCatalogNode;

public class NameSorter extends ViewerSorter {
	
	public int compare(Viewer viewer, Object e1, Object e2) {
		int tmp = getType(e1) - getType(e2); 
		if (tmp == 0) {
			LabelProviderImplementation labelProvider = new LabelProviderImplementation();
			return labelProvider.getText(e1).compareTo(labelProvider.getText(e2));
		}
		return tmp;
	}
	
	private int getType(Object e) {
		if (e instanceof FileNode) {
			return 2;
		}
		if ((e instanceof CatalogNode) && (e instanceof MultiplyCatalogNode)) {
			return 1;
		}
		if (e instanceof GridNode) {
			return 0;
		}
		return -1;
	}

}
